/* 
 * Copyright 2011 VelocitiSoftware
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.velociti.ikarus.widget.client.ui;

import com.google.gwt.event.dom.client.DomEvent.Type;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Image;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.Paintable;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.ui.ClickEventHandler;
/**
 * ######VIkarusDockItem##### 
 * -CSide widget used in VIkarusDock
 * @author Alper Turkyilmaz - VelocitiSoftware - 2011
 * @version 1.0.2
 * 
 */
public class VIkarusDockItem extends Image implements Paintable {
	public static final String CLICK_EVENT_IDENTIFIER = "click";
	ApplicationConnection client;
	String id;
	String title = "";
	private final ClickEventHandler clickEventHandler = new ClickEventHandler(this, CLICK_EVENT_IDENTIFIER) {
		protected <H extends EventHandler> HandlerRegistration registerHandler(H handler, Type<H> type) {
			return addDomHandler(handler, type);
		}
	};

	public VIkarusDockItem() {
	}

	public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
		this.client = client;
		id = uidl.getId();
		setTitle(title);
		if (client.updateComponent(this, uidl, true)) {
			return;
		}
		setUrl(getSrc(uidl, client));
		if (uidl.hasAttribute("title")) {
			title = uidl.getStringAttribute("title");
			setTitle(title);
		}
		clickEventHandler.handleEventHandlerRegistration(client);
	}

	private String getSrc(UIDL uidl, ApplicationConnection client) {
		String url = client.translateVaadinUri(uidl.getStringAttribute("src"));
		if (url == null) {
			return "";
		}
		return url;
	}

}
